﻿// ApplicationSettings.cs
// (c) 2011, Charles Lechasseur
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace LaunchWithParams
{
    //************************************************************************
    /// <summary>
    /// Class allowing access to all settings related to the LaunchWithParams
    /// application itself.
    /// </summary>
    //************************************************************************
    public static class ApplicationSettings
    {
        /// Path of registry key containing the global settings for LaunchWithParams. Contains the HKLM prefix.
        private const string LAUNCHWITHPARAMS_GLOBAL_REG_PATH = @"HKEY_LOCAL_MACHINE\Sofware\clechasseur\LaunchWithParams";

        /// Path of registry key containing the per-user settings for LaunchWithParams. Contains the HKCU prefix.
        private const string LAUNCHWITHPARAMS_USER_REG_PATH = @"HKEY_CURRENT_USER\Software\clechasseur\LaunchWithParams";

        /// Name of setting controlling the saving of executable settings in the registry.
        private const string STORE_WITH_FULL_PATH_SETTING = "StoreSettingsWithFullPath";

        /// Maximum size of the MRU and Pinned lists for an executable.
        private const string MRU_LIST_SIZE_SETTING = "MaxMRUListSize";

        /// Default value for the list sizes property.
        private const int DEFAULT_MRU_LIST_SIZES = 5;

        /// <summary>
        /// Path of executable to launch once the user chooses the
        /// command-line parameters.
        /// </summary>
        public static string ExeToLaunch
        {
            get;
            internal set;
        }

        /// <summary>
        /// Determines how the registry key storing exe settings
        /// are created. If this property is set to <c>true</c>,
        /// the full path of the executable will be used (if available).
        /// If set to <c>false</c>, only the file name of the
        /// executable will be used.
        /// </summary>
        public static bool StoreExeSettingsWithFullPath
        {
            get {
                bool storeWithFullPaths = false;
                object setting = GetSetting(STORE_WITH_FULL_PATH_SETTING);
                if (setting != null) {
                    if (setting is Boolean) {
                        storeWithFullPaths = (Boolean) setting;
                    } else if (setting is Int32) {
                        storeWithFullPaths = ((Int32) setting) != 0;
                    }
                }
                return storeWithFullPaths;
            }
        }

        /// <summary>
        /// Determines the maximum number of command-lines that can be stored in
        /// an executable's list of most-recently used command-lines.
        /// </summary>
        public static int SizeOfCommandLineLists
        {
            get {
                int sizes = DEFAULT_MRU_LIST_SIZES;
                object setting = GetSetting(MRU_LIST_SIZE_SETTING);
                if (setting != null && setting is Int32) {
                    sizes = (Int32) setting;
                }
                return sizes;
            }
        }

        //************************************************************************
        /// <summary>
        /// Retrieves the value for a LaunchWithParams setting. Will first look
        /// in the per-user registry key, then in the global key if not found.
        /// </summary>
        /// <param name="name">Name of setting (e.g. registry value).</param>
        /// <returns>Setting as found in registry, or <c>null</c> if not found.</returns>
        //************************************************************************
        private static object GetSetting(string name)
        {
            object setting = Registry.GetValue(LAUNCHWITHPARAMS_USER_REG_PATH, name, null);
            if (setting == null) {
                setting = Registry.GetValue(LAUNCHWITHPARAMS_GLOBAL_REG_PATH, name, null);
            }
            return setting;
        }
    }
}
